package com.example.pan.http

import android.util.Log
import androidx.paging.Pager
import androidx.paging.PagingConfig
import com.example.pan.model.AccountInfo
import com.example.pan.ui.file.DriveFileDataSource
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import javax.inject.Inject

class PanRepository @Inject constructor(
    private val service: PanService
) {

    suspend fun list(
        token: String,
        dir: String,
        order: String,
        desc: String,
        start: Int,
        num: Int
    ) = service.list(token, dir, order, desc, start, num)

    fun list(dir: String, token: String) = Pager(
        config = PagingConfig(1000),
        pagingSourceFactory = { DriveFileDataSource(service, dir, token) }
    ).flow


    @ExperimentalCoroutinesApi
    fun filemeta(fs_id: Long) = flow {
        val list = service.filemetas(arrayListOf(fs_id).toString())
        if (list.list.isNotEmpty()) {
            emit(list.list[0])
        }
    }.catch {
        it.message?.let { it1 -> Log.e("http", it1) }
    }.flowOn(Dispatchers.IO)
}